const http = require('http')

const WHITE_LIST = ['127.0.0.1', 'localhost']
http
  .createServer(function(request, response) {
    console.info('request come', request.url)
    const origin = request.headers.origin
    const flag = WHITE_LIST.some(i => new RegExp(i).test(origin))
    response.writeHead(200, {
      'Content-type': 'application/json',
      'Access-Control-Allow-Origin': flag ? origin : '',
      'Access-Control-Allow-Methods': 'PUT, DELETE',
      'Access-Control-Allow-Headers': 'x-ccc, x-bbb',
      'Access-Control-Allow-Max-Age': 2 * 1000000000000
    })
    response.end(`{"hello": "wrold"}`)
  })
  .listen('9999')

console.log('server listen 9999')
